﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.AccountManagement;

namespace ICeFramework.Common.ActiveDirectory
{
    [Serializable]
    public abstract class ADObject
    {
        #region protected Principal _objectPrincipal
        /// <summary>
        /// Get/Sets the _objectPrincipal of the <see cref="Common.ActiveDirectory.ADObject"/>
        /// </summary>
        protected Principal _objectPrincipal;
        #endregion

        private string _name;
        #region public string Name
        /// <summary>
        /// Get/Sets the Name of the ADObject
        /// </summary>
        /// <value></value>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        #endregion

        private string _displayName;
        #region public string DisplayName
        /// <summary>
        /// Get/Sets the DisplayName of the ADObject
        /// </summary>
        /// <value></value>
        public string DisplayName
        {
            get { return _displayName; }
            set { _displayName = value; }
        }
        #endregion

        #region public object Tag
        /// <summary>
        /// Get/Sets the Tag of the ADObject
        /// </summary>
        /// <value></value>
        public object Tag
        {
            get;
            set;
        }
        #endregion

        private ADContextType _contextType;
        #region public ADContextType ContextType
        /// <summary>
        /// Get/Sets the ContextType of the ADObject
        /// </summary>
        /// <value></value>
        public ADContextType ContextType
        {
            get { return _contextType; }
            set { _contextType = value; }
        }
        #endregion

        #region public abstract ADObjectType ObjectType
        /// <summary>
        /// Gets the ObjectType of the ADObject
        /// </summary>
        /// <value></value>
        public abstract ADObjectType ObjectType
        {
            get;
        }
        #endregion

        #region public ADObject()
        /// <summary>
        /// Initializes a new instance of the <b>ADObject</b> class.
        /// </summary>
        public ADObject()
            : this(string.Empty, ADContextType.Unknown, null)
        {

        }
        #endregion

        #region public ADObject(string name, ADContextType contextType, Principal objectPrincipal)
        /// <summary>
        /// Initializes a new instance of the <b>ADObject</b> class.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="contextType"></param>
        /// <param name="objectPrincipal"></param>
        public ADObject(string name, ADContextType contextType, Principal objectPrincipal)
        {
            _name = name;
            _contextType = contextType;
            _objectPrincipal = objectPrincipal;
        }
        #endregion

        #region public IList<GroupInfo> GetGroups()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<GroupInfo> GetGroups()
        {
            return GetGroups(ContextType);
        }
        #endregion

        #region public IList<GroupInfo> GetGroups(ADContextType contextType)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="contextType"></param>
        /// <returns></returns>
        public IList<GroupInfo> GetGroups(ADContextType contextType)
        {
            List<GroupInfo> info = new List<GroupInfo>();
            if (_objectPrincipal == null)
            {
                return info;
            }

            ContextType userType = (ContextType)Enum.Parse(typeof(ContextType), contextType.ToString(), true);
            PrincipalContext userContext = new PrincipalContext(userType);

            PrincipalSearchResult<Principal> sr = _objectPrincipal.GetGroups();
            foreach (Principal princ in sr)
            {
                if (princ is GroupPrincipal)
                {
                    info.Add(GroupInfo.GetGroupInfo(princ as GroupPrincipal, ContextType));
                }
            }

            return info;
        }
        #endregion
    }
}
